<?php
session_start();
if (!isset($_SESSION['authenticated']) || !$_SESSION['authenticated']) {
    header('Location: auth.php'); // 如果未登录，重定向到登录页面
    exit();
}
?>

<?php
$dbFile = '../cms.db';
$pdo = new PDO('sqlite:' . $dbFile);
$posts = $pdo->query("SELECT * FROM posts")->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>乂蛙博客 管理后台</title>
<link rel="shortcut icon" href="http://tc.gobghui.icu/uploads/66aefd85f0edc.png">
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="../css/mdui.min.css">
</head>
<body class="mdui-theme-primary-blue mdui-theme-accent-blue">
    <div class="mdui-container">
        <h1>管理后台</h1>
        <a href="add.php" class="mdui-btn mdui-btn-raised">添加新文章</a>
        <a href="logout.php" class="mdui-btn mdui-btn-danger">注销</a>
        <ul class="mdui-list">
            <?php foreach ($posts as $post): ?>
                <li class="mdui-list-item">
                    <div class="mdui-list-item-content">
                        <strong><?php echo htmlspecialchars($post['title']); ?></strong>
                        <a href="edit.php?id=<?php echo $post['id']; ?>" class="mdui-btn mdui-btn-icon mdui-color-blue-100 mdui-btn-flat"><i class="mdui-icon material-icons">&#xe3c9;</i></a>
                        <a href="delete.php?id=<?php echo $post['id']; ?>" class="mdui-btn mdui-btn-icon mdui-color-teal-100 mdui-btn-flat"><i class="mdui-icon material-icons">&#xe872;</i></a>
                    </div>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <script src="../js/mdui.min.js"></script>
</body>
</html>
